﻿using Absky.Entity.SystemManage;
using Absky.Service.SystemManage;
using Absky.Util;
using Absky.Model;
using Absky.Web.Controllers;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Threading.Tasks;

namespace Absky.Web.Areas.SystemModule.Controllers
{
    [Area("SystemModule")]
    public class LogoImgController : BaseController
    {
        private readonly LogoImgService logoImgService = new LogoImgService();
        #region 视图功能
        /// <summary>
        /// PC端图片设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AuthorizeOnly("system:role:view")]
        public IActionResult PCIndex()
        {
            return View();
        }
        /// <summary>
        /// 移动端图片设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AppIndex()
        {
            return View();
        }
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取头像
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        [HttpGet]
        [AuthorizeOnly("system:role:search,organization:user:search")]
        public async Task<IActionResult> GetImg(string code)
        {
            //logoImgIBLL.GetImg(code);
            //return Success("获取成功。");
            return await Task.FromResult(Json(code));
        }
        /// <summary> 
        /// 获取实体数据 
        /// </summary> 
        /// <param name="keyValue">主键</param> 
        /// <returns></returns> 
        public async Task<LogoImgEntity> GetEntity(string keyValue)
        {
            return await logoImgService.GetEntity(keyValue);
        }
        public async Task<FileStreamResult> GetLogo(string code)
        {
            LogoImgEntity entity = await GetEntity(code);
            string fileHeadImg = GlobalContext.Configuration.GetSection("UploadConfig:FileLogoImg").Value;
            string fileImg;
            if (entity != null)
            {
                fileImg = string.Format("{0}/{1}{2}", fileHeadImg, entity.F_Code, entity.F_FileName);
            }
            else
            {
                fileImg = Path.Combine(fileHeadImg, code + ".png");// string.Format("{0}/{1}.png", fileHeadImg, code);
            }
            FileInfo fileInfo = new FileInfo(fileImg);
            var stream = new FileStream(fileImg, FileMode.Open, FileAccess.Read, FileShare.Read);
            return new FileStreamResult(stream, "image/png");
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 上传头像
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AuthorizeOnly("system:role:add,system:role:edit")]
        public async Task<IActionResult> UploadFile(string code)
        {
            //HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            ////没有文件上传，直接返回
            //if (files[0].ContentLength == 0 || string.IsNullOrEmpty(files[0].FileName))
            //{
            //    return HttpNotFound();
            //}

            //string FileEextension = Path.GetExtension(files[0].FileName);
            //string fileHeadImg = Config.GetValue("fileLogoImg");
            //string fullFileName = string.Format("{0}/{1}{2}", fileHeadImg, code, FileEextension);
            ////创建文件夹，保存文件
            //string path = Path.GetDirectoryName(fullFileName);
            //Directory.CreateDirectory(path);
            //files[0].SaveAs(fullFileName);

            //LogoImgEntity entity = new LogoImgEntity();
            //entity.F_Code = code;
            //entity.F_FileName = FileEextension;
            //logoImgIBLL.SaveEntity(entity);
            //return Success("上传成功。");
            return await Task.FromResult(Json(code));
        }
        #endregion
    }
}
